package com.zzyl.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.zzyl.entity.AccraditationRecord;
import com.zzyl.mapper.AccraditationRecordMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.AccraditationRecordService;
import com.zzyl.vo.RecordVo;
import com.zzyl.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccraditationRecordServiceImpl implements AccraditationRecordService {

    @Autowired
    private AccraditationRecordMapper accraditationRecordMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;
    /**
     * 保存审批操作记录
     * @param recordVo
     */
    @Override
    public void insert(RecordVo recordVo) {
        AccraditationRecord acc = new AccraditationRecord();
        acc.setOpinion(recordVo.getOption());//审批意见
        acc.setType(recordVo.getType());//审批类型
        acc.setApproverId(recordVo.getUserId());//审批人id
        acc.setApproverName(recordVo.getRealName());//审批人姓名
        acc.setBussniessId(recordVo.getId());//业务id
        acc.setCurrentStep(recordVo.getStep());//当前审核步骤
        acc.setAuditStatus(recordVo.getStatus());//审批状态
        acc.setHandleType(recordVo.getHandleType());//处理类型 只有院长是审批操作,其余人都是处理操作

        //寻找下一个审核人
        Long nextAssignee = recordVo.getNextAssignee();
        if(ObjectUtil.isNotEmpty(nextAssignee)){
            List<UserRoleVo> userRoleVos = userRoleMapper.selectByUserId(nextAssignee);
            if(ObjectUtil.isNotEmpty(userRoleVos)){
                UserRoleVo userRoleVo = userRoleVos.get(0);
                //下一个审批人角色
                acc.setNextApproverRole(userRoleVo.getRoleName());
                //下一个审批人
                acc.setNextApprover(userRoleVo.getUserName());
                //下一个审批人id
                acc.setNextApproverId(userRoleVo.getId());
                //下一个审核步骤
                acc.setNextStep(recordVo.getNextStep());
            }

        }




        accraditationRecordMapper.insert(acc);
    }

    /**
     * 查询操作记录
     * @param taskId 任务ID
     * @param type 业务类型
     * @return 操作记录
     */
    @Override
    public List<AccraditationRecord> getAccraditationRecordByBuisId(Long taskId, Integer type) {
       return accraditationRecordMapper.getAccraditationRecordByBuisId(taskId,type);
    }
}
